# Makefile for compiler project
# SysY 词法分析器构建和测试自动化

.PHONY: all build build-release clean test test-unit test-integration run help format check clippy

# 默认目标
all: build

# 构建项目
build:
	@echo "构建调试版本..."
	cargo build

# 构建发布版本
build-release:
	@echo "构建发布版本..."
	cargo build --release

# 清理构建产物
clean:
	@echo "清理构建产物..."
	cargo clean

# 运行所有测试
test: test-unit test-integration

# 运行单元测试
test-unit:
	@echo "运行单元测试..."
	cargo test

# 运行集成测试
test-integration: build-release
	@echo "运行集成测试..."
	@bash tests/run_tests.sh

# 运行词法分析器（需要指定文件）
# 使用方法: make run FILE=tests/sample1.in
run: build-release
	@if [ -z "$(FILE)" ]; then \
		echo "错误: 请指定输入文件，例如: make run FILE=tests/sample1.in"; \
		exit 1; \
	fi
	@echo "分析文件: $(FILE)"
	@./target/release/compiler $(FILE)

# 生成测试输出文件
generate-outputs: build-release
	@echo "生成测试输出文件..."
	@bash tests/generate_outputs.sh

# 代码格式化
format:
	@echo "格式化代码..."
	cargo fmt --all

# 代码检查
check:
	@echo "检查代码语法..."
	cargo check

# 代码质量检查
clippy:
	@echo "运行 clippy 代码质量检查..."
	cargo clippy -- -D warnings

# 完整的代码质量检查流程
quality: format clippy check test

# 显示帮助信息
help:
	@echo "SysY 词法分析器 - 可用命令:"
	@echo ""
	@echo "构建命令:"
	@echo "  build           构建调试版本"
	@echo "  build-release   构建发布版本"
	@echo "  clean           清理构建产物"
	@echo ""
	@echo "测试命令:"
	@echo "  test            运行所有测试"
	@echo "  test-unit       运行单元测试"
	@echo "  test-integration 运行集成测试"
	@echo "  generate-outputs 生成测试输出文件"
	@echo ""
	@echo "运行命令:"
	@echo "  run FILE=<file> 运行词法分析器分析指定文件"
	@echo "                  例如: make run FILE=tests/sample1.in"
	@echo ""
	@echo "代码质量:"
	@echo "  format          格式化代码"
	@echo "  check           检查代码语法"
	@echo "  clippy          运行代码质量检查"
	@echo "  quality         完整的代码质量检查流程"
	@echo ""
	@echo "其他:"
	@echo "  help            显示此帮助信息"